<?php
/**
 * Created by PhpStorm.
 * User: iamya
 * Date: 2017/10/10
 * Time: 8:56
 */

namespace app\circle\controller;


use app\circle\model\CircleMemberModel;
use app\circle\model\CompanyErrorModel;
use app\circle\model\CompanyModel;
use app\circle\model\MemberAttentionModel;
use app\circle\model\MemberModel;
use app\circle\model\MemberReportModel;
use app\circle\model\ThemeAttentionModel;
use app\circle\model\ThemeModel;
use think\Request;
use think\Validate;

class MemberController extends BaseController
{
    private $memberId;
    private $memberInfo;
    private $memberModel;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        if ($this->islogin()<=0){
            $this->error('请先登陆',url('Public/userLogin',['reback'=>get_current_url()]));
        }
        $this->memberModel=new MemberModel();
        $this->memberId=$this->getMemberId();
        $this->memberInfo=$this->memberModel->where('member_id',$this->memberId)->find();
        $this->assign('memberInfo',$this->memberInfo);
    }
/*会员首页*/
    public function index()
    {

        return $this->fetch();
    }
/*编辑会员信息*/
    public function userinfo(){
        if ($this->request->isPost()){
            $data=$this->request->param();
            $validate=new Validate([
                ['member_truename','require','请填写姓名'],
                ['member_duties','requireWith:company_id','请输入职位']
            ]);
            if (!$validate->check($data)){
                $this->error($validate->getError());
            }
            $data['member_id']=$this->memberId;
            if ($this->memberModel->editData($data)){
                $this->success('编辑成功','index');
            }else{
                $this->error('编辑失败');
            }
        }
        return $this->fetch();
    }
    /*编辑企业信息*/
    public function companyinfo(){
        $companyModel=new CompanyModel();
        $companyInfo=$companyModel->where('company_id',$this->memberInfo['company_id'])->find();
        $this->assign('companyInfo',$companyInfo);
        return $this->fetch();
    }

    public function companyError()
    {
        $data=$this->request->param();
        $validate=new Validate([
            ['company_id','require','公司不明确'],
            ['error_msg','require','请输入报错信息']
        ]);
        if (!$validate->check($data)){
            $this->error($validate->getError());
        }
        $data['member_id']=$this->memberId;
        $model=new CompanyErrorModel();
        if ($model->addData($data)){
            $this->success('报错成功');
        }else{
            $this->error('报错失败');
        }
    }

    public function theme()
    {
        return $this->fetch();
    }

    public function attention()
    {
        $type=$this->request->param('type',0);
        $map=[
            't2.member_id'=>$this->memberId,
            'type'=>$type
        ];

        $themeModel=new ThemeModel();
        $list = $themeModel->where($map)->alias('t1')
            ->join('__CIRCLE_THEME_ATTENTION__ t2','t1.theme_id=t2.attention_theme_id')
            ->order('create_time desc')->paginate(10);
        $attentionModel=new ThemeAttentionModel();
        foreach ($list as $index => $value) {
            $list[$index]['is_like'] = $value['is_like'];
            $list[$index]['like_list'] = $value['like_list'];
            $list[$index]['reply'] = $value['reply'];
            $list[$index]['theme_image'] = $value['theme_image'];
            $list[$index]['theme_content']['text'] = html_out($value['theme_content']['text']);
            $list[$index]['create_time'] = date('Y-m-d H:i:s', $value['create_time']);
            $list[$index]['is_attention']=$attentionModel->where(['member_id'=>$this->memberId,'attention_theme_id'=>$value['theme_id'],'type'=>0])->count();

        }
        if ($this->request->isAjax()) {
            return $list;
        }
        $this->assign('list', $list);
        if ($type){
            $title='我的屏蔽';
        }else{
            $title='我的关注';
        }
        $this->assign('title',$title);
        return $this->fetch();
    }

    public function memberAttention()
    {
        $memberId=$this->request->param('member_id',0);
        $memberInfo=$this->memberModel->where('member_id',$memberId)->count();
        if (empty($memberInfo)){
            $this->error('未找到对应用户');
        }
        $data=[
            'member_id'=>$this->memberId,
            'attention_member_id'=>$memberId
        ];
        $model=new MemberAttentionModel();
        if ($model->where($data)->delete()){
            $this->success('取消关注用户成功');
        }else{
            if ($model->addData($data)){
                $this->success('关注用户成功');
            }else{
                $this->error('操作失败');
            }
        }
    }

    public function report()
    {
        $model=new MemberReportModel();
        $list=$model->where('member_id',$this->memberInfo['member_id'])->order('create_time desc')->paginate();
        if ($this->request->isAjax()){
            return $list;
        }
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function apply()
    {
        $model=new CircleMemberModel();
        $list=$model->where('member_id',$this->memberInfo['member_id'])->order('cm_applytime desc')->paginate();
        if ($this->request->isPost()){
            return $list;
        }
        $this->assign('list',$list);
        return $this->fetch();
    }
}